/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.manage.service.impl;

import me.zhengjie.modules.manage.domain.HouseEle;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.manage.repository.HouseEleRepository;
import me.zhengjie.modules.manage.service.HouseEleService;
import me.zhengjie.modules.manage.service.dto.HouseEleDto;
import me.zhengjie.modules.manage.service.dto.HouseEleQueryCriteria;
import me.zhengjie.modules.manage.service.mapstruct.HouseEleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yyj
* @date 2023-09-05
**/
@Service
@RequiredArgsConstructor
public class HouseEleServiceImpl implements HouseEleService {

    private final HouseEleRepository houseEleRepository;
    private final HouseEleMapper houseEleMapper;

    @Override
    public Map<String,Object> queryAll(HouseEleQueryCriteria criteria, Pageable pageable){
        Page<HouseEle> page = houseEleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(houseEleMapper::toDto));
    }

    @Override
    public List<HouseEleDto> queryAll(HouseEleQueryCriteria criteria){
        return houseEleMapper.toDto(houseEleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public HouseEleDto findById(String id) {
        HouseEle houseEle = houseEleRepository.findById(id).orElseGet(HouseEle::new);
        ValidationUtil.isNull(houseEle.getId(),"HouseEle","id",id);
        return houseEleMapper.toDto(houseEle);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HouseEleDto create(HouseEle resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return houseEleMapper.toDto(houseEleRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(HouseEle resources) {
        HouseEle houseEle = houseEleRepository.findById(resources.getId()).orElseGet(HouseEle::new);
        ValidationUtil.isNull( houseEle.getId(),"HouseEle","id",resources.getId());
        houseEle.copy(resources);
        houseEleRepository.save(houseEle);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            houseEleRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<HouseEleDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (HouseEleDto houseEle : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("家具家电名字", houseEle.getName());
            map.put("类型1家具2家电", houseEle.getType());
            map.put("功能参数", houseEle.getParam());
            map.put("过期时间", houseEle.getExpireTime());
            map.put("质保年限", houseEle.getYear());
            map.put("负责人电话", houseEle.getDutyTel());
            map.put("负责人", houseEle.getDutyPerson());
            map.put("品牌", houseEle.getBrand());
            map.put("来源", houseEle.getSource());
            map.put("文件", houseEle.getPicPath());
            map.put("价格", houseEle.getPrice());
            map.put("创建人", houseEle.getCreateBy());
            map.put("创建时间", houseEle.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}